﻿angular.module('app.masterdata.contactor', [])

    .controller('contactorEditCtrl', ['$scope', 'cont', '$modalInstance', 'num', 'site', 'langs', 'isHoldStatus', 'contactProxy',
        'legal',
    function ($scope, cont, $modalInstance, num, site, langs, isHoldStatus, contactProxy,legal) {

        $scope.tocclist = [
          { "id": '1', "toccName": 'To' },
          { "id": '2', "toccName": 'Cc' }
        ];
        $scope.isGroupList = [
          { "id": 0, "level": 'No' },
          { "id": 1, "level": 'Yes' }
        ];
        if (cont.id == null) {
        //    cont.customerNum = num;
            cont.deal = site;
        }
        cont.customerNum = num;
        $scope.languagelist = langs;
        $scope.holdlist = isHoldStatus;
        $scope.legallist = legal;
        if (cont.communicationLanguage == null) {
            cont.communicationLanguage = langs[0].detailValue;
        } else {
            cont.communicationLanguage = cont.communicationLanguage;
        }
        if (cont.isDefaultFlg == null) {
            cont.isDefaultFlg = isHoldStatus[1].detailValue;
        } else {
            cont.isDefaultFlg = cont.isDefaultFlg;
        }

        if (cont.legalEntity == null) {
            cont.legalEntity = "All";
        } else {
            cont.legalEntity = cont.legalEntity;
        }
        if (cont.toCc==null) {
            cont.toCc = "1";
        }
        if (cont.groupCode == null || cont.groupCode == "" ) {
            cont.isGroupLevel = 0;
        } else {
            cont.isGroupLevel = 1;
        }

        $scope.cont = cont;


        $scope.closeContact = function () {
            $modalInstance.close();
        };

        //$scope.onSave = function () {
        //    $modalInstance.close();
        //};

        $scope.updateCommon = function () {
            if ($scope.cont.legalEntity==null) {
                $scope.cont.legalEntity = "All";
            }
            //if ($scope.cont.isGroupLevel=0) {
            //    $scope.cont.customerNum = $scope.bkcustomerNum;
            //}
                contactProxy.updateContact($scope.cont, function () { $modalInstance.close(); },
                    function (res) {
                        alert(res);
                    });
            //}
        };

    }]);

